package com.bibrApp.difu;

import android.app.Application;
import android.content.Context;
import android.content.Intent;
import android.util.Log;

import com.bibrApp.difu.Response.LoginResponseBody;
import com.bibrApp.difu.api.SocketIOClient;
import com.scwang.smartrefresh.header.MaterialHeader;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.footer.ClassicsFooter;
import com.tencent.bugly.crashreport.CrashReport;

import java.net.URISyntaxException;
import java.security.KeyManagementException;
import java.security.NoSuchAlgorithmException;
import java.security.SecureRandom;

import javax.net.ssl.SSLContext;
import javax.net.ssl.TrustManager;

import androidx.appcompat.app.AppCompatDelegate;

import io.socket.client.IO;
import io.socket.client.Socket;
import io.socket.emitter.Emitter;
import okhttp3.OkHttpClient;

public class DfApp extends Application {
    public static String BASE_URL;
    private LoginResponseBody mData;//登录返回数据
    private String userId;//userId
    private String uId;//memberId
    private String token;
    private Context mContext;
    private IO.Options mIOOptions;
    private Socket mSocket;
    String nickName;
    String nickImage;
    String memberId;
    private String xpub;
    private String blance;
    private String coinType;

    public String getCoinType() {
        return coinType;
    }

    public void setCoinType(String coinType) {
        this.coinType = coinType;
    }

    public String getBlance() {
        return blance;
    }

    public void setBlance(String blance) {
        this.blance = blance;
    }

    public String getuAccountName() {
        return uAccountName;
    }

    private String uAccountName;

    public void setuAccountName(String uAccountName) {
        this.uAccountName = uAccountName;
    }

    public String getXpub() {
        return xpub;
    }

    public void setXpub(String xpub) {
        this.xpub = xpub;
    }

    public String getMemberId() {
        return memberId;
    }

    public void setMemberId(String memberId) {
        this.memberId = memberId;
    }

    public String getNickImage() {
        return nickImage;
    }

    public void setNickImage(String nickImage) {
        this.nickImage = nickImage;
    }

    public String getNickName() {
        return nickName;
    }

    public void setNickName(String nickName) {
        this.nickName = nickName;
    }

    public Context getmContext() {
        return mContext;
    }

    public LoginResponseBody getmData() {
        return mData;
    }

    public void setmData(LoginResponseBody mData) {
        this.mData = mData;
    }

    public String getUserId() {
        return userId;
    }

    public void setUserId(String userId) {
        this.userId = userId;
    }

    public String getuId() {
        return uId;
    }

    public void setuId(String uId) {
        this.uId = uId;
    }

    public String getToken() {
        return token;
    }

    public void setToken(String token) {
        this.token = token;
    }

    @Override
    public void onCreate() {
        super.onCreate();
        mContext = this;
        CrashReport.initCrashReport(getApplicationContext(), "fc8d8dc5b3", false);
    }

    static {
        AppCompatDelegate.setCompatVectorFromResourcesEnabled(true);
        SmartRefreshLayout.setDefaultRefreshHeaderCreator((context, layout) -> {
            layout.setPrimaryColorsId(R.color.colorAccent, R.color.colorAccent);//全局设置主题颜色

            return new MaterialHeader(context).setColorSchemeResources(R.color.colorAccent);
        });
        //设置全局的Footer构建器
        SmartRefreshLayout.setDefaultRefreshFooterCreator((context, layout) -> {
            //指定为经典Footer，默认是 BallPulseFooter
            ClassicsFooter footer = new ClassicsFooter(context);
            footer.setTextSizeTitle(12);
            footer.setDrawableSize(22);
            return footer;
        });

    }

    {
        try {

            SocketIOClient.BiBrTrustManager biBrTrustManager = new SocketIOClient.BiBrTrustManager();
            SecureRandom secureRandom = new SecureRandom();

            SSLContext sslContext;
            sslContext = SSLContext.getInstance("TLS");
            sslContext.init(null, new TrustManager[]{biBrTrustManager}, secureRandom);

            OkHttpClient okHttpClient = new OkHttpClient.Builder()
                    .hostnameVerifier(new SocketIOClient.BiBrHostnameVerifier())
                    .sslSocketFactory(sslContext.getSocketFactory(), biBrTrustManager)
                    .build();

            IO.setDefaultOkHttpCallFactory(okHttpClient);
            IO.setDefaultOkHttpWebSocketFactory(okHttpClient);
            mIOOptions = new IO.Options();

            mIOOptions.transports = new String[]{"websocket"};
            mIOOptions.webSocketFactory = okHttpClient;
            mIOOptions.callFactory = okHttpClient;
            mIOOptions.reconnection = true;
            mIOOptions.secure = true;
//            //地址和端口
            mSocket = IO.socket(Config.SOCKET_URL, mIOOptions);
        } catch (NoSuchAlgorithmException e) {
            e.printStackTrace();
        } catch (KeyManagementException e) {
            e.printStackTrace();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }


    public Socket getSocket() {
        try {
            return IO.socket(Config.SOCKET_URL, mIOOptions);
        } catch (URISyntaxException e) {
            e.printStackTrace();
            return null;
        }
    }

    private static final String TAG = "socket";
    private Emitter.Listener onConnect = new Emitter.Listener() {
        @Override
        public void call(Object... args) {
            Log.e(TAG, "Connected");
        }
    };

    private Emitter.Listener onDisconnect = new Emitter.Listener() {
        @Override
        public void call(Object... args) {
            Log.e(TAG, "onDisConnected");

        }
    };

    private Emitter.Listener onConnectError = new Emitter.Listener() {
        @Override
        public void call(Object... args) {
            Log.e(TAG, "Error connecting");
            Log.e(TAG, "msg:" + args[0].toString());
        }
    };

    public void socketOnEvent(Socket socket) {
        if (socket == null) {
            return;
        }
        socket.on(Socket.EVENT_CONNECT, onConnect);
        socket.on(Socket.EVENT_DISCONNECT, onDisconnect);
        socket.on(Socket.EVENT_CONNECT_ERROR, onConnectError);
        socket.connect();
    }

    public void socketOffEvent(Socket socket) {
        if (socket == null) {
            return;
        }
        socket.off(Socket.EVENT_CONNECT, onConnect);
        socket.off(Socket.EVENT_DISCONNECT, onDisconnect);
        socket.off(Socket.EVENT_CONNECT_ERROR, onConnectError);
        socket.disconnect();
    }
}
